<template>
  <common-card
    title="累计用户数"
    :value="userToday">
    <template>
      <!--      <div id="total-users-chart" style="width: 100%;height : 100%;"></div>-->
      <v-chart :options="totalUsers"></v-chart>
    </template>
    <template v-slot:footer>
      <div class="total-users-footer">
        <span>日同比</span>
        <span class="emphasis">{{ userGrowthLastDay }}</span>
        <div class="increase"/>
        <span class="month">月同比</span>
        <span class="emphasis">{{ userGrowthLastMonth }}</span>
        <div class="decrease"/>
      </div>
    </template>
  </common-card>
</template>

<script>
import commonCardMixin from '@/mixins/commonCardMixin'
import { pct, format } from '@/utils'
import { totalUsers } from '@/api/axiosHttp'

export default {
  name: 'TotalUsers',
  mixins: [commonCardMixin],
  data () {
    return {
      userGrowthLastDay: 0,
      userGrowthLastMonth: 0,
      userTodayNumber: 0,
      userLastMonth: 0,
      userToday: 0
    }
  },
  computed: {
    totalUsers () {
      return {
        grid: {
          left: 0,
          right: 0,
          top: 0,
          bottom: 0
        },
        xAxis: {
          type: 'value',
          show: false
        },
        yAxis: {
          type: 'category',
          show: false

        },
        series: [{
          type: 'bar',
          data: [this.userLastMonth],
          barWidth: '20%',
          stack: '总量',
          itemStyle: { color: '#45c946' }
        }, {
          type: 'bar',
          data: [this.userTodayNumber],
          barWidth: '25%',
          stack: '总量',
          itemStyle: {
            color: '#eee'
          }
        }, {
          type: 'custom',
          stack: '总量',
          data: [this.userLastMonth],
          renderItem (params, api) {
            const value = api.value(0)
            const endPoint = api.coord([value, 0])

            return {
              type: 'group',
              position: endPoint,
              children: [{
                type: 'path',
                shape: {
                  d: 'M1024 255.996 511.971 767.909 0 255.996 1024 255.996z',
                  x: -5,
                  y: -20,
                  width: 10,
                  height: 10,
                  layout: 'cover'
                },
                style: {
                  fill: '#45c946'
                }
              }, {
                type: 'path',
                shape: {
                  d: 'M0 767.909l512.029-511.913L1024 767.909 0 767.909z',
                  x: -5,
                  y: 10,
                  width: 10,
                  height: 10,
                  layout: 'cover'
                },
                style: {
                  fill: '#45c946'
                }
              }]
            }
          }
        }]
      }
    }
  },
  created () {
    totalUsers().then(data => {
      const {
        userGrowthLastDay,
        userGrowthLastMonth,
        userTodayNumber,
        userLastMonth,
        userToday
      } = data
      this.userGrowthLastDay = pct(userGrowthLastDay)
      this.userGrowthLastMonth = pct(userGrowthLastMonth)
      this.userTodayNumber = userTodayNumber
      this.userLastMonth = userLastMonth
      this.userToday = format(userToday)
    })
  }
}
</script>

<style scoped lang="scss">
.total-users-footer {
  display: flex;
  align-items: center;

  .month {
    margin-left: 10px;
  }
}
</style>
